﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Entity;
using Ones.Dal.Repository.Interfaces;

namespace Ones.Dal.Repository.Impl
{
    public class JobAssignmentDao : AbstractDao<JobAssignment>, IJobAssignmentDao
    {
        public override bool
        update(JobAssignment jobAssignment)
        {
            try
            {
                var curr = getById(jobAssignment.jaId);

                curr.empId = jobAssignment.empId;
                curr.divId = jobAssignment.divId;
                curr.jobId = jobAssignment.jobId;
                curr.jaDateModify = jobAssignment.jaDateModify;
                curr.jaDateStart = jobAssignment.jaDateStart;
                curr.jaDateFinish = jobAssignment.jaDateFinish;

                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public override JobAssignment
        getById(object id)
        {
            return getSingle(i => i.jaId == (int)id);
        }

        public JobAssignment
        getById(string empId, string divId, DateTime dateTime)
        {
            try
            {
                var q = from ja in _ctx.JobAssignments
                        where ja.empId.Equals(empId) &&
                              ja.divId.Equals(divId) &&
                              (
                               ja.jaDateStart.Year == dateTime.Year &&
                               ja.jaDateStart.Month == dateTime.Month &&
                               ja.jaDateStart.Date == dateTime.Date)
                        select ja;
                return q.Single<JobAssignment>();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<JobAssignment>
        getByEmployee(string empId)
        {
            try
            {
                return getMultiple(i => i.empId.Equals(empId));
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<JobAssignment>
        getByDivision(string divId)
        {
            try
            {
                return getMultiple(i => i.divId.Equals(divId));
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<JobAssignment>
        getByJob(string jobId)
        {
            try
            {
                return getMultiple(i => i.jobId.Equals(jobId));
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<JobAssignment>
        getByDateStart(DateTime dateTime)
        {
            try
            {
                return getMultiple(i => i.jaDateStart.Equals(dateTime));
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<JobAssignment>
        getByDateFinish(DateTime dateTime)
        {
            try
            {
                return getMultiple(i => i.jaDateFinish.Equals(dateTime));
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

    #region IJobAssignmentDao

    public interface IJobAssignmentDao : IDao<JobAssignment>
    {
        JobAssignment getById(String empId, String divId, DateTime dateTime);

        List<JobAssignment> getByEmployee(String empId);

        List<JobAssignment> getByDivision(String divId);

        List<JobAssignment> getByJob(String jobId);

        List<JobAssignment> getByDateStart(DateTime dateTime);

        List<JobAssignment> getByDateFinish(DateTime dateTime);
    }
    #endregion
}